/** new style clients and contacts **/
function loadPage() {
	var ajaxResp = ajaxJSONtoPOST(Object.toJSON({process : "combos"}), serviceDemand);
	if(ajaxResp.Flag=="true") {
		var htmlCode = ajaxResp.HTMLCode;
		/*
		alert(htmlCode.split("~")[0]);
		alert(htmlCode.split("~")[1]);
		alert(htmlCode.split("~")[1]);
		*/
		$("#cdPhone_1 .lbl").html(htmlCode.split("~")[0]);
		$("#ctdPhone_1 .lbl").html(htmlCode.split("~")[0]);
		
		$("#cdMail_1 .lbl").html(htmlCode.split("~")[1]);
		$("#ctdMail_1 .lbl").html(htmlCode.split("~")[1]);
		
		$("#cdTech_1 .lbl").html(htmlCode.split("~")[2]);
		$("#crdClient_1 .lblcombo").html(htmlCode.split("~")[3]);
		
		$("#ct-rContainer").css("display", "none");
	}
}

function saveContact() {
	//alert("Save->"+$("#newContactID").val());
	var tipo = $("#newContactID").val()==""?1:2;
	
	var nombre 		= $("#ctiName_1").val();
	var apaterno 	= $("#ctiPatname_1").val();
	var amaterno 	= $("#ctiMatname_1").val();
	var direccion 	= $("#ctiUbicacion_1").val();
	
	var arrayPhone = new Array();
	var arrayMail = new Array();
	
	var chPh = $("#ctdPhone").children();
	arrayPhone = getDataChilds(chPh);
	
	var chMl = $("#ctdMail").children();
	arrayMail =  getDataChilds(chMl);
	
	var jsonReq = {
			process : "cContact",
			type	: tipo,
			sType	: 4,
			name	: nombre,
			paterno : apaterno,
			materno : amaterno,
			idContact : $("#newContactID").val(),
			idClient  : $("#newClientID").val(),
			aPhone	: arrayPhone,
			aMail 	: arrayMail,
			address	: direccion,
			eMode	: 0
	};
	
	//alert(Object.toJSON(jsonReq));
	
	/****/
	var jsonResp = ajaxJSONtoPOST(Object.toJSON(jsonReq), serviceDemand);
	if(jsonResp.Flag="true") {
		showEntitiesContact(jsonResp.HTMLCode);
		if(tipo==1) {
			alert("Contacto creado correctamente");
		} else {
			alert("Contacto actualizado correctamente");
		}
		editClient($("#newClientID").val());
		$(".dialog").dialog("close");
	}
	/*****/
}

function saveClient() {
	
	var name 	= $("#ciName_1").val();
	var address = $("#ciAddress_1").val();
	var phone 	= $("#ciPhone_1").val();
	var tech 	= $("#ciTech_1").val();
	var web 	= $("#ciWeb_1").val();
	var arrayEmpty = new Array();
	if(!validateFields(name, 1))	{ arrayEmpty.push("Nombre"); }
	if(!validateFields(address, 1))	{ arrayEmpty.push("Direcci\u00f3n"); }
	if(!validateFields(phone, 1))	{ arrayEmpty.push("Tel\u00e9fono"); }
	if(!validateFields(tech, 1))	{ arrayEmpty.push("Tecnolog\u00eda"); }
	
	if(arrayEmpty.length==0) {
		/**
		 * type -> 	1-Guardar | 3-Actualizar
		 * stype -> 4-Guardar 
		 */
		var type =  $("#newClientID").val()==""?1:3;  
		//alert(type);
		
		var arrayPhone = new Array();
		var arrayMail = new Array();
		var arrayTech = new Array();
		
		var chPh = $("#cdPhone").children();
		arrayPhone = getDataChilds(chPh);
		
		var chMl = $("#cdMail").children();
		arrayMail = getDataChilds(chMl);
		
		var chTc = $("#cdTech").children();
		arrayTech = getDataChilds(chTc);
		
		var jsonObj = {
				process		: "createClient",
				type		: type,
				sType		: 4,
				nombre 		: name,
				direccion 	: address,
				pagina		: web, 
				telefono 	: arrayPhone,
				mail		: arrayMail,
				tech		: arrayTech,
				id			: $("#newClientID").val(),
				alta		: 0,
				eMode		: 0,
				estatus		: ""
		};
		
		//alert(Object.toJSON(jsonObj));
		/***/
		var jsonResp = ajaxJSONtoPOST(Object.toJSON(jsonObj), serviceDemand);
		if(jsonResp.Flag="true") {
			$("#newClientID").val(jsonResp.idGenerated);
			showEntitiesClient(jsonResp.HTMLCode);
			
			if(type==1) {
				alert("Cliente creado correctamente");
			} else {
				alert("Cliente actualizado correctamente");
			}
		}
		/****/
	} else {
		alert("Favor de llenar los campos obligatorios: "+getMsgEmptyData(arrayEmpty));
	}
}

function showEntitiesClient(htmlCode) {
	
	$("#cdPhone").html(htmlCode.split("~")[0]);
	$("#cdMail").html(htmlCode.split("~")[1]);
	$("#cdTech").html(htmlCode.split("~")[2]);
	
	var arrayParents = new Array();
	arrayParents.push("cdPhone");
	arrayParents.push("cdMail");
	arrayParents.push("cdTech");
	
	showByParents(arrayParents);
}

function showEntitiesContact(htmlCode) {
	
	$("#ctdPhone").html(htmlCode.split("~")[0]);
	$("#ctdMail").html(htmlCode.split("~")[1]);
	var arrayParents = new Array();
	arrayParents.push("ctdPhone");
	arrayParents.push("ctdMail");
	
	showByParents(arrayParents);
}

function dropEntitieClient(div) {
	var dbIDEntitie = $("#"+div+" .dbID").val();
	
	if(dbIDEntitie != "0") {
		var drop = false;
		var jsonReq = {
			process :  "deleteEntitie",
			entitie :	div,
			dbId	:	dbIDEntitie
		};
		
		jsonResp = ajaxJSONtoPOST(Object.toJSON(jsonReq), serviceDemand);
		//alert(jsonResp.Flag);
		if(jsonResp.Flag=="true") {
			drop = true;
		}
	} else {
		drop = true;
	}
	return drop;
}

function refreshDialog() {
	//alert(formParentDialog);
	if(formParentDialog=="c-form") {
		
		$("#ctiName_1").val("");
		$("#ctiPatname_1").val("");
		$("#ctiMatname_1").val("");
		$("#ctiUbicacion_1").val("");
		
		$("#ctiPhone_1").val("");
		$("#ctdPhone_1 .dbID").val("0");
		
		$("#ctiMail_1").val("");
		$("#ctdMail_1 .dbID").val("0");
		
		var chPhones = $("#ctdPhone").children();
		var lengthChilds = chPhones.length;
		removeComponent(chPhones, lengthChilds);
		
		var chMails = $("#ctdMail").children();
		lengthChilds = chMails.length;
		removeComponent(chMails, lengthChilds);
	}
	else if(formParentDialog=="ct-form") {
		$("#ctiName_1").val("");
		$("#ctiPatname_1").val("");
		$("#ctiMatname_1").val("");
		$("#ctiUbicacion_1").val("");
		
		$("#ctiPhone_1").val("");
		$("#ctdPhone_1 .dbID").val("0");
		
		$("#ctiMail_1").val("");
		$("#ctdMail_1 .dbID").val("0");
		
		var chPhones = $("#ctdPhone").children();
		var lengthChilds = chPhones.length;
		removeComponent(chPhones, lengthChilds);
		
		var chMails = $("#ctdMail").children();
		lengthChilds = chMails.length;
		removeComponent(chMails, lengthChilds);
		
		//alert($("#newClientID").val());
		editClient($("#newClientID").val());
	}
	
	$("#newContactID").val("");
}

function removeComponent(cmpArray, lengthChilds) {
	var lblSpan = "#"+cmpArray[0].id+" .lbl .lblSpan";
	var lblCombo = "#"+cmpArray[0].id+" .lbl .combobox";
	
	$(lblCombo).css("display", "none");
	$(lblSpan).css("display", "inline");
	
	for(var index = 1; index < lengthChilds; index ++){
		var actualDiv = "#"+cmpArray[index].id;
		$(actualDiv).remove();
	}
}

/**
 * Reporte de Clientes
 */
function findClients(idClient) {
	var combo = $("#crdClient_1 .lblcombo .combo option:selected").val();
	var comboSts = $("#crdStatus_1 .lblcombo .combo option:selected").val();
	if(validateFields($("#criNoCl_1").val(), 1) ||
			validateFields(combo, 6) ||
			validateFields($("#criDate_1").val(), 1) ||
			validateFields(comboSts, 6)) {
		var jsonReq = {
				process	 	: "sClient",
				id			: idClient==0 ? $("#criNoCl_1").val() : idClient,
				nombre		: combo!="Selecciona"?combo:"",
				alta		: $("#criDate_1").val(),
				estatus		: comboSts!="Selecciona"?comboSts:"",
				direccion 	: "",
				pagina		: "",
				mail		: new Array(),
				telefono	: new Array(),
				tech		: new Array(),
				eMode		: idClient==0 ? 0 : 1
		};
		var jsonResp = ajaxJSONtoPOST(Object.toJSON(jsonReq), serviceDemand);
		if(jsonResp.Flag=="true") {
			if(idClient==0) {
				$("#tblReportClients").html(jsonResp.HTMLCode);
				loadTableCSS("tblClientsReport");
			} else {
				$("#newClientID").val(jsonResp.Id);
				$("#ciName_1").val(jsonResp.Nombre);
				$("#ciAddress_1").val(jsonResp.Direccion);
				$("#ciWeb_1").val(jsonResp.Web);
				showEntitiesClient(jsonResp.HTMLCode);
				loadClientsTab(0);
				
				
				$("#ct-rContainer").css("display", "inline");
				$("#ct-report").html(jsonResp.HTMLContacts);
				loadTableCSS("tblContactsReport");
			}
		}
	} else {
		alert("Favor de llenar al menos un filtro");
	}
}

function editClient(elID) {
	findClients(elID);
}

function editContact(clID) {
	var params = {
			process : "sContact",
			type	: 3,
			sType	: 0,
			name	: "",
			paterno : "",
			materno : "",
			idContact : clID,
			idClient  : 0,
			aPhone	: new Array(),
			aMail 	: new Array(),
			address : "",
			eMode	: 1
	};
	
	jsonReq = ajaxJSONtoPOST(Object.toJSON(params), serviceDemand);
	if(jsonReq.Flag=="true") {
		$(".dialog").dialog("open");
		formParentDialog = "ct-form";
		$("#newContactID").val(clID);
		
		$("#ctiName_1").val(jsonReq.name);
		$("#ctiPatname_1").val(jsonReq.patt);
		$("#ctiMatname_1").val(jsonReq.matt);
		$("#ctiUbicacion_1").val(jsonReq.add);
		
		showEntitiesContact(jsonReq.HTMLCode);
	}
}